﻿using UnityEngine;
using System.Collections;

public class TweenMonsterShied : MonoBehaviour {

	UISlider _hp;
	float _nowvalue = 1;
	float _targetValue;
	float _addSpeed = 0.1f;
	float reSpeed;
	public VoidDelegate OnShiedZero;
	public bool IsHPZero{
		get{ return _hp.value <= 0.01f && _targetValue == 0f; }
	}

	FSM _hpstate;
	StatePath _add = new StatePath("out");
	StatePath _sleep = new StatePath("sleep");

	void Awake()
	{

		_hp = GetComponent<UISlider>();

		InitState();

	}
	public void InitState() {

		_hpstate = new FSM();
		_hpstate.AddState(_add, OnOutEnter, OnOutUpdate);
		_hpstate.AddState(_sleep, OnSleepEnter);
	}
	public void Stop(int target){
		_targetValue = target;
		_nowvalue = target;
		_hpstate.SetState (_sleep);
	}
	void OnOutEnter()
	{

		UpdateValue();
	}

	void OnOutUpdate(float dt)
	{

		_nowvalue = Mathf.SmoothDamp(_nowvalue, _targetValue, ref reSpeed, _addSpeed);
		UpdateValue();
	}

	void OnSleepEnter()
	{
	}

	public void SetHPValue(float value)
	{
		_targetValue = value;
		if (_targetValue == 0) {
			if(OnShiedZero!= null){
				OnShiedZero ();
			}
		}
		_hpstate.SetState(_add);
	}


	void UpdateValue()
	{

		_hp.value = _nowvalue;
	}


	void Update()
	{
		_hpstate.UpdateState(Time.deltaTime);
	}
}
